package com.pshcp.api;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.ao.common.CommonAO;
import com.pshcp.ao.message.MessageNotificationPageAO;
import com.pshcp.domain.dto.MessageNotificationDTO;
import com.pshcp.domain.model.MessageNotification;
import com.pshcp.service.MessageNotificationService;
import com.pshcp.tool.MessageTool;
import com.pshcp.vo.message.MessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 通用接口-系统消息
 * @Author: xiaoche
 * @Date: 2022/11/4 21:34
 */
@Api(tags = {"通用接口-系统消息"})
@Slf4j
@RestController
@RequestMapping("/api/message")
public class MessageAPI {

    @Autowired
    private MessageNotificationService messageNotificationService;

    @ApiOperation("获取系统消息列表(分页)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取系统消息列表(分页)")
    @PostMapping("getMessageNotificationPage")
    public ResultDTO<PageList<MessageVO>> getMessageNotificationPage(@RequestBody MessageNotificationPageAO messageNotificationPageAO) throws BizException {
        MessageNotificationDTO messageNotificationDTO = MessageTool.buildGetMessageNotificationPage(messageNotificationPageAO);
        PageList<MessageNotification> pageList = messageNotificationService.list(messageNotificationDTO);
        PageList<MessageVO> page = MessageTool.buildGetMessageNotificationPage(pageList);
        return ResultBuilder.success(page);
    }

    @ApiOperation("全部已读")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "全部已读")
    @PostMapping("readAllMessageNotification")
    public ResultDTO<Long> readAllMessageNotification() throws BizException {
        messageNotificationService.readAllMessageNotification(ReqComTool.getUserType(), ReqComTool.getUserId());
        return ResultBuilder.success(null);
    }

    @ApiOperation("阅读系统消息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "阅读系统消息")
    @PostMapping("readMessageNotification")
    public ResultDTO<Long> readMessageNotification(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        MessageNotification messageNotification = messageNotificationService.findById(commonAO.getId());
        CommonTool.checkObj(messageNotification, "90000007");
        if (!messageNotification.getUserId().equals(ReqComTool.getUserId())) {
            return ResultBuilder.failure("90000013");
        }
        MessageNotification readMessageNotification = MessageTool.buildReadMessageNotification(messageNotification);
        messageNotificationService.update(readMessageNotification);
        return ResultBuilder.success(null);
    }

    @ApiOperation("获取未阅读系统消息数量")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取未阅读系统消息数量")
    @PostMapping("getUnReadMessageNotificationCount")
    public ResultDTO<Integer> getUnReadMessageNotificationCount() throws BizException {
        int unReadMessageNotificationCount = messageNotificationService.getUnReadMessageNotificationCount(ReqComTool.getUserType(), ReqComTool.getUserId());
        return ResultBuilder.success(unReadMessageNotificationCount);
    }

    @ApiOperation("删除系统消息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "删除系统消息")
    @PostMapping("delMessageNotification")
    public ResultDTO<Long> delMessageNotification(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        MessageNotification messageNotification = messageNotificationService.findById(commonAO.getId());
        CommonTool.checkObj(messageNotification, "90000007");
        if (!messageNotification.getUserId().equals(ReqComTool.getUserId())) {
            return ResultBuilder.failure("90000013");
        }
        MessageNotification readMessageNotification = MessageTool.buildDelMessageNotification(messageNotification);
        messageNotificationService.update(readMessageNotification);
        return ResultBuilder.success(readMessageNotification.getId());
    }

    @ApiOperation("全部删除系统消息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "全部删除系统消息")
    @PostMapping("delAllMessageNotification")
    public ResultDTO<Long> delAllMessageNotification() throws BizException {
        messageNotificationService.delAllMessageNotification(ReqComTool.getUserType(), ReqComTool.getUserId());
        return ResultBuilder.success(null);
    }
}
